package edu.ncsu.helloandroid;

import android.app.Activity;
import android.content.Intent;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;

public abstract class FGCActivity extends Activity {
	
	//Custom RESULT values must be greater than 1
	public static final int RESULT_FGC_EXIT = 2;
	
	public static final int RESULT_FGC_KEY_PRESS = 3;
	public static final String KEY_PRESS_ID = "FGC_KEY_PRESS";
	
	public static final int CREATE_COURSE_ACTIVITY = 0;
	public static final int CREATE_SOCIAL_NETWORK_ACTIVITY = 1;
	public static final int CREATE_ADVICE_ACTIVITY = 2;
	public static final int CREATE_SCORECARD_ACTIVITY = 3;
	


	
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
    	getMenuInflater().inflate(R.menu.fgc_options_menu, menu);

    	menu.findItem(R.id.fgc_ScorecardItem).setVisible(showScorecardItem());
    	
    	return super.onCreateOptionsMenu(menu);
    }
    
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
    	
    	
    	switch(item.getItemId()){
    	case R.id.fgc_FacebookFriendsItem:
    	case R.id.fgc_OnlineScoresItem:
    		Intent onlineIntent = new Intent(this,FGCOnline.class);
    		startActivityForResult(onlineIntent, CREATE_SOCIAL_NETWORK_ACTIVITY);
    		break;
    	case R.id.fgc_ScorecardItem:
    		closeOptionsMenu();
    		Intent scorecardIntent = new Intent(this,FGCScorecard.class);
    		startActivityForResult(scorecardIntent,CREATE_SCORECARD_ACTIVITY);
    		break;
    	
    	case R.id.fgc_QuitItem:
    		quitFGC();
    		break;
    		
		default:
			break;
    		
    	}
    	return super.onOptionsItemSelected(item);
    }
    
    protected void quitFGC(){
 	   setResult(RESULT_FGC_EXIT);
 	   finish();
    }
    
	protected boolean showScorecardItem() {
		return FGCGameState.getGameState(getTaskId()).isPlaying();
	}
    
    protected void dismissOnKeyPress(int keyCode, KeyEvent event){
	    if (keyCode == KeyEvent.KEYCODE_T || keyCode == KeyEvent.KEYCODE_P) { //throwing or walking
	    	setResult(RESULT_FGC_KEY_PRESS);
            Intent resultValue = new Intent();
            resultValue.putExtra(KEY_PRESS_ID, keyCode);
	    	setResult(RESULT_FGC_KEY_PRESS, resultValue);
	    	finish();
	    }
    }
    
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		switch (resultCode) {
		case RESULT_FGC_EXIT:
			setResult(RESULT_FGC_EXIT);
	 	   	finish();
			break;
		case RESULT_FGC_KEY_PRESS:
			onKeyDown(data.getExtras().getInt(FGCActivity.KEY_PRESS_ID),null);
			break;
		default:
			break;
		}
		
	}

    
    

}
